package com.omazan.nativeClient;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;

import com.omazan.db.HandleProductTypes;
import com.omazan.db.HandleProducts;
import com.omazan.db.models.Customer;
import com.omazan.db.models.Product;
import com.omazan.db.models.ProductType;
import com.omazan.utils.OmazanException;

public class ProdOperations {
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
	
	public void addProd() throws OmazanException, IOException{
		
			
			Product p1 = new Product();
			System.out.println("Enter Product Name:");
			p1.setProductName(input.readLine());
			System.out.println("Enter Product Description:");
			p1.setProductDesc(input.readLine());
			System.out.println("Enter Product Price:");
			p1.setPrice(Float.parseFloat(input.readLine()));
			System.out.println("Enter Product Discount if any:");
			p1.setDiscount(Float.parseFloat(input.readLine()));
			HandleProducts hp1 = new HandleProducts();
			ProdTypeOperations d = new ProdTypeOperations();
			System.out.println("Product Type ID Catalogue:");
			d.dispProdType();
			HandleProductTypes t = new HandleProductTypes();
			System.out.println("Select Product Type ID from above:");
			p1.setProductType(t.readProductTypeFromDBUsingId(Integer.parseInt(input.readLine())));
			hp1.insertProduct(p1);
	}
	
public void delProd() throws OmazanException, IOException{
		
		
		HandleProducts hp2 = new HandleProducts();
		
		List<Product> listofProducts = hp2
				.readAllProductsFromDB();
		
		for (Product eachProduct : listofProducts) {
			System.out.println(eachProduct.getProductName() + "|"
					+ eachProduct.getProductDesc()+ "|"
							+ eachProduct.getPrice()+ "|"
									+ eachProduct.getDiscount()+ "|"
											+ eachProduct.getProductType());
		}

		System.out.println("Choose Product by ID to delete:");
		int pIdToDel = Integer.parseInt(input.readLine());
		System.out
		.println("Are you sure you want to delete this Product?");
		char choice=(char)input.read();
		if(choice=='y'||choice=='Y')
		{
		
		String delp = hp2.readProductFromDBUsingId(pIdToDel)
				.getProductName();
		hp2.deleteUsingId(pIdToDel);
		new HandleProductTypes().deleteUsingId(pIdToDel);

		System.out.println("Product :" + delp
				+ " Deleted from Product Database.");}
		else {System.out.println("Delete Aborted.");}
		
		
}
public void dispProd() throws OmazanException, IOException {
	
	
	HandleProductTypes hpt2 = new HandleProductTypes();
	
	List<ProductType> listofProducttypes = hpt2.readAllProductTypesFromDB();
	
	for (ProductType eachProductType : listofProducttypes)
	{
		System.out.println("----------------------------------------");
		System.out.println(eachProductType.getProductType());
		System.out.println("----------------------------------------");
		int prodmatchid = eachProductType.getProductTypeId();
		HandleProducts hpt1 = new HandleProducts();
		List<Product> listofProducts = hpt1.readAllProductsFromDB();
	for (Product eachProduct : listofProducts) {
		
		if(prodmatchid==eachProduct.getProductType().getProductTypeId()){
		System.out.println(eachProduct.getProductName() + "|"
				+ eachProduct.getProductDesc()+ "|"
						+ eachProduct.getPrice()+ "|"
								+ eachProduct.getDiscount()+ "|"
										+ eachProduct.getProductId());
	}}
	}

}
}
